Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Works with chrome but not safari (?<![\n])[\n]{1}(?![\n])

I have this regex (?<![\n])[\n]{1}(?![\n]) in my Javascript code, and as I understand Safari just doesn't support negative look behinds.

What I need to do is replace all \n that are not surrounded by other \n and replace them with a string.

Can someone please help me figure out how to write that Regex so that it is usable in Safari?

Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could capture either the start of the string or a non whitespace character in group 1.

Then match a single newline, and assert not a newline to the right.

In the replacement use the capture group followed by your replacement string $1[replacement]

Note that you don't need the square brackets around the newline, and you can omit {1}

(\S|^)\n(?!\n)

Regex demo

const regex = /(\S|^)\n(?!\n)/g;
const str = `
this is a
this is b
this is c

d


test

test


test
test
`;

const result = str.replace(regex,`$1[replacement]`);

console.log(result);

Your pattern with the lookbehind (if supported):

const regex = /(?<!\n)\n(?!\n)/g;
const str = `
this is a
this is b
this is c

d


test

test


test
test
`;
`[replacement]`
const result = str.replace(regex,`[replacement]`);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Use

text.replace(/(\n)?\n(?!\n)/g, function(p,q) {
    return q === undefined ? "<a string>":p
})

JS code:

var text = "a\nb\n\nc"
console.log(
  text.replace(/(\n)?\n(?!\n)/g, function(p,q) {
    return q === undefined ? "<a string>":p
}))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda